package gui;

import java.awt.BorderLayout;
import java.awt.EventQueue;

import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.border.EmptyBorder;
import javax.swing.JOptionPane;
import javax.swing.JTabbedPane;
import javax.swing.JLabel;
import javax.swing.JTextField;
import javax.swing.JButton;

import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;

import javax.swing.JScrollPane;
import javax.swing.JTextArea;

import java.awt.Color;

import javax.swing.JEditorPane;

public class game extends JFrame {

	private JPanel contentPane;
	private JTabbedPane tabbedPane;
	private JPanel panel;
	private JPanel panel_1;
	private JPanel panel_2;
	private JLabel label;
	private JTextField textField;
	private JButton btnNewButton;

	private JScrollPane scrollPane;
	private JTextArea textArea;

	private JPanel panel_3;
	private JLabel lblNewLabel;
	private JPanel panel_4;
	private JPanel panel_5;
	private JLabel label_1;
	private JPanel panel_6;
	private JLabel label_2;
	private JLabel label_3;
	private JTextField textField_1;
	private JButton button;
	private JScrollPane scrollPane_1;
	private JLabel lblNewLabel_1;
	private JScrollPane scrollPane_2;
	private JTextArea result;
	private JButton btnNewButton_1;
	private JButton button_1;
	private JButton button_2;
	private JPanel panel_7;
	private JLabel label_4;
	private JButton btnNewButton_2;
	private JButton btnUp;
	private JButton btnDown;
	private JButton btnYes;
	private JButton btnNo;
	private JTextArea comoutput;
	private JScrollPane scrollPane_3;
	private JTextArea res;
	private JButton button_3;
	private JButton button_4;
	private JButton button_5;
	private JLabel lblNewLabel_2;
	private JTextArea turn;
	private JButton btnNewButton_3;

	int ran = (int) (Math.random() * 1000) + 1;
	int count = 0;
	boolean f = true;
	
	
	String user = "";
	int su;


	int max = 1000; // 최대값
	int min = 1; // 최소값
	int ran2 = max / 2; //컴퓨터가 묻는값(중간값)
	int count2 = 0; // 시도횟수

	/**
	 * Launch the application.
	 */
	public static void main(String[] args) {
		EventQueue.invokeLater(new Runnable() {
			public void run() {
				try {
					game frame = new game();
					frame.setVisible(true);
				} catch (Exception e) {
					e.printStackTrace();
				}
			}
		});
	}

	/**
	 * Create the frame.
	 */
	public game() {
		setTitle("Game");
		setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		setBounds(550, 230, 323, 357);
		contentPane = new JPanel();
		contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
		setContentPane(contentPane);
		contentPane.setLayout(null);
		contentPane.add(getTabbedPane());
		btnUp.setEnabled(false); // 버튼 비활성화
		btnDown.setEnabled(false);

	}

	private JTabbedPane getTabbedPane() {
		if (tabbedPane == null) {
			tabbedPane = new JTabbedPane(JTabbedPane.TOP);
			tabbedPane.setBounds(0, 0, 307, 319);
			tabbedPane.addTab("스무고개", null, getPanel(), null);
			tabbedPane.addTab("가위바위보", null, getPanel_1(), null);
			tabbedPane.addTab("묵찌빠", null, getPanel_2(), null);
			tabbedPane.addTab("스무고개2", null, getPanel_4(), null);
		}
		return tabbedPane;
	}

	private JPanel getPanel() {
		if (panel == null) {
			panel = new JPanel();
			panel.setLayout(null);
			panel.add(getLabel());
			panel.add(getTextField());
			panel.add(getBtnNewButton());
			panel.add(getScrollPane());
			panel.add(getPanel_3_1());
		}
		return panel;
	}

	private JPanel getPanel_1() {
		if (panel_1 == null) {
			panel_1 = new JPanel();
			panel_1.setLayout(null);
			panel_1.add(getPanel_6());
			panel_1.add(getScrollPane_2());
			panel_1.add(getBtnNewButton_1());
			panel_1.add(getButton_1());
			panel_1.add(getButton_2());
			panel_1.add(getBtnNewButton_2());
		}
		return panel_1;
	}

	private JPanel getPanel_2() {
		if (panel_2 == null) {
			panel_2 = new JPanel();
			panel_2.setLayout(null);
			panel_2.add(getPanel_7());
			panel_2.add(getScrollPane_3());
			panel_2.add(getButton_3());
			panel_2.add(getButton_4());
			panel_2.add(getButton_5());
			panel_2.add(getLblNewLabel_2());
			panel_2.add(getTurn());
		}
		return panel_2;
	}

	private JLabel getLabel() {
		if (label == null) {
			label = new JLabel("정답은? (1~1000)");
			label.setBounds(12, 30, 108, 15);
		}
		return label;
	}

	private JTextField getTextField() {
		if (textField == null) {
			textField = new JTextField();
			textField.setBounds(33, 55, 116, 21);
			textField.setColumns(10);
		}
		return textField;
	}

	private JButton getBtnNewButton() {
		if (btnNewButton == null) {
			btnNewButton = new JButton("확 인");
			btnNewButton.addActionListener(new ActionListener() {
				public void actionPerformed(ActionEvent arg0) {
					try {
						if (Integer.parseInt(textField.getText()) > 0
								&& Integer.parseInt(textField.getText()) <= 1000) {
							if (f == false) {
								textArea.setText(" ");
								count = 0;
								ran = (int) (Math.random() * 1000) + 1;
								f = true;
							}

							int in = Integer.parseInt(textField.getText());
							textField.setText("");
							if (in < ran) {
								textArea.append(in + "보다 큽니다. \n");
								count++;
							}

							if (in > ran) {
								textArea.append(in + "보다 작습니다. \n");
								count++;
							}

							if (in == ran) {
								count++;
								textArea.append(in + "정답 입니다.\n");
								textArea.append(count + "번 \n");
								textField.setText("");
								f = false;
							}
						} else {
							JOptionPane.showMessageDialog(null,
									"1~1000 사이의 숫자를 입력해주세요.");
						}

					} catch (Exception e) {
						JOptionPane.showMessageDialog(null, "숫자를 입력해주세요.");
					}
				}
			});
			btnNewButton.setBounds(161, 54, 97, 23);
		}
		return btnNewButton;
	}

	private JScrollPane getScrollPane() {
		if (scrollPane == null) {
			scrollPane = new JScrollPane();
			scrollPane.setBounds(22, 111, 249, 169);
			scrollPane.setViewportView(getTextArea());
		}
		return scrollPane;
	}

	private JTextArea getTextArea() {
		if (textArea == null) {
			textArea = new JTextArea();
		}
		return textArea;
	}

	private JPanel getPanel_3_1() {
		if (panel_3 == null) {
			panel_3 = new JPanel();
			panel_3.setBackground(new Color(0, 139, 139));
			panel_3.setBounds(0, 0, 302, 20);
			panel_3.add(getLblNewLabel());
		}
		return panel_3;
	}

	private JLabel getLblNewLabel() {
		if (lblNewLabel == null) {
			lblNewLabel = new JLabel("스무고개");
			lblNewLabel.setForeground(new Color(255, 255, 255));
		}
		return lblNewLabel;
	}

	private JPanel getPanel_4() {
		if (panel_4 == null) {
			panel_4 = new JPanel();
			panel_4.setLayout(null);
			panel_4.add(getPanel_5());
			panel_4.add(getLabel_3());
			panel_4.add(getTextField_1());
			panel_4.add(getButton());
			panel_4.add(getScrollPane_1());
			panel_4.add(getLblNewLabel_1());
			panel_4.add(getBtnUp());
			panel_4.add(getBtnDown());
			panel_4.add(getBtnYes());
			panel_4.add(getBtnNo());
			panel_4.add(getBtnNewButton_3());
		}
		return panel_4;
	}

	private JPanel getPanel_5() {
		if (panel_5 == null) {
			panel_5 = new JPanel();
			panel_5.setBackground(new Color(0, 139, 139));
			panel_5.setBounds(0, 0, 302, 20);
			panel_5.add(getLabel_1());
		}
		return panel_5;
	}

	private JLabel getLabel_1() {
		if (label_1 == null) {
			label_1 = new JLabel("스무고개2");
			label_1.setForeground(Color.WHITE);
		}
		return label_1;
	}

	private JPanel getPanel_6() {
		if (panel_6 == null) {
			panel_6 = new JPanel();
			panel_6.setBounds(0, 0, 302, 20);
			panel_6.setBackground(new Color(0, 139, 139));
			panel_6.add(getLabel_2());
		}
		return panel_6;
	}

	private JLabel getLabel_2() {
		if (label_2 == null) {
			label_2 = new JLabel("가위바위보");
			label_2.setForeground(Color.WHITE);
		}
		return label_2;
	}

	private JLabel getLabel_3() {
		if (label_3 == null) {
			label_3 = new JLabel("1~1000 입력");
			label_3.setBounds(12, 30, 86, 15);
		}
		return label_3;
	}

	private JTextField getTextField_1() {
		if (textField_1 == null) {
			textField_1 = new JTextField();
			textField_1.setColumns(10);
			textField_1.setBounds(33, 55, 116, 21);
		}
		return textField_1;
	}

	private JButton getButton() {
		if (button == null) {
			button = new JButton("확 인");
			button.addActionListener(new ActionListener() {
				public void actionPerformed(ActionEvent arg0) {
					count2++;

					try {// try-catch 를 사용하여 문자를 입력하였을시 오류메시지 표시

						// 0~1000 사이의 숫자를 입력했을때 실행
						if (Integer.parseInt(textField_1.getText()) > 0
								&& Integer.parseInt(textField_1.getText()) <= 1000) {

						comoutput.append(ran2 + "이(가) 정답입니까??\n");

						} else {
							JOptionPane.showMessageDialog(null,
									"1~1000 사이의 숫자를 입력해주세요.");
						}

					} catch (Exception e) {
						JOptionPane.showMessageDialog(null, "숫자를 입력해주세요.");
					}

				}
			});
			button.setBounds(161, 54, 97, 23);
		}
		return button;
	}

	private JScrollPane getScrollPane_1() {
		if (scrollPane_1 == null) {
			scrollPane_1 = new JScrollPane();
			scrollPane_1.setBounds(33, 113, 225, 54);
			scrollPane_1.setViewportView(getComoutput());
		}
		return scrollPane_1;
	}

	private JLabel getLblNewLabel_1() {
		if (lblNewLabel_1 == null) {
			lblNewLabel_1 = new JLabel("컴퓨터입력값");
			lblNewLabel_1.setBounds(12, 97, 86, 15);
		}
		return lblNewLabel_1;
	}

	private JScrollPane getScrollPane_2() {
		if (scrollPane_2 == null) {
			scrollPane_2 = new JScrollPane();
			scrollPane_2.setBounds(10, 31, 280, 158);
			scrollPane_2.setViewportView(getResult());
		}
		return scrollPane_2;
	}

	private JTextArea getResult() {
		if (result == null) {
			result = new JTextArea();
		}
		return result;
	}

	private JButton getBtnNewButton_1() {
		if (btnNewButton_1 == null) {
			btnNewButton_1 = new JButton("가위");
			btnNewButton_1.addActionListener(new ActionListener() {
				public void actionPerformed(ActionEvent arg0) {
					String com;

					su = (int) (Math.random() * 3); // 0~2까지 중 랜덤 수 추출

					switch (su) {
					case 0:
						com = "가위";
						break; // su가 0이면 컴퓨터가 가위를 낸 것으로 한다.
					case 1:
						com = "바위";
						break; // su가 1이면 컴퓨터가 바위를 낸 것으로 한다.
					default:
						com = "보";
						break; // 기본적으로 컴퓨터가 보를 낸 것으로 한다.
					}

					user = "가위"; // 유저가 가위를 낸 경우

					if (com.equals("보")) {
						result.append("가위를 내다니! 당신이 이겼습니다.(com:" + com + ")\n");

					} else if (com.equals("가위")) {
						result.append("비겼군요. 다시 도전해주세요.(com:" + com + ")\n");

					} else {
						result.append("제가 졌습니다..(com:" + com + ")\n");
					}
				}

			});
			btnNewButton_1.setBounds(8, 199, 86, 51);
		}
		return btnNewButton_1;
	}

	private JButton getButton_1() {
		if (button_1 == null) {
			button_1 = new JButton("바위");
			button_1.addActionListener(new ActionListener() {
				public void actionPerformed(ActionEvent arg0) {
					String com;
					su = (int) (Math.random() * 3);
					switch (su) {
					case 0:
						com = "가위";
						break;
					case 1:
						com = "바위";
						break;
					default:
						com = "보";
						break;
					}

					user = "바위"; // 유저가 바위를 낸 경우

					if (com.equals("가위")) {
						result.append("바위를 내다니! 당신이 이겼습니다.(com:" + com + ")\n");
					} else if (com.equals("바위")) {
						result.append("비겼군요. 다시 도전해주세요.(com:" + com + ")\n");
					} else {
						result.append("제가 졌습니다..(com:" + com + ")\n");
					}
				}

			});
			button_1.setBounds(106, 199, 86, 51);
		}
		return button_1;
	}

	private JButton getButton_2() {
		if (button_2 == null) {
			button_2 = new JButton("보");
			button_2.addActionListener(new ActionListener() {
				public void actionPerformed(ActionEvent arg0) {
					String com;
					su = (int) (Math.random() * 3);
					switch (su) {
					case 0:
						com = "가위";
						break;
					case 1:
						com = "바위";
						break;
					default:
						com = "보";
						break;
					}

					user = "보"; // 유저가 보를 낸 경우

					if (com.equals("바위")) {
						result.append("보를 내다니! 당신이 이겼습니다.(com:" + com + ")\n");
					} else if (com.equals("보")) {
						result.append("비겼군요. 다시 도전해주세요.(com:" + com + ")\n");
					} else {
						result.append("제가 졌습니다..(com:" + com + ")\n");
					}
				}
			});
			button_2.setBounds(204, 199, 86, 51);
		}
		return button_2;
	}

	private JPanel getPanel_7() {
		if (panel_7 == null) {
			panel_7 = new JPanel();
			panel_7.setBackground(new Color(0, 139, 139));
			panel_7.setBounds(0, 0, 302, 20);
			panel_7.add(getLabel_4());
		}
		return panel_7;
	}

	private JLabel getLabel_4() {
		if (label_4 == null) {
			label_4 = new JLabel("묵찌빠");
			label_4.setForeground(Color.WHITE);
		}
		return label_4;
	}

	private JButton getBtnNewButton_2() {
		if (btnNewButton_2 == null) {
			btnNewButton_2 = new JButton("화면 지우기!");
			btnNewButton_2.addActionListener(new ActionListener() {
				public void actionPerformed(ActionEvent arg0) {
					result.setText(" ");

				}
			});
			btnNewButton_2.setBounds(10, 257, 278, 23);
		}
		return btnNewButton_2;
	}

	private JButton getBtnUp() {
		if (btnUp == null) {
			btnUp = new JButton("Up");
			btnUp.addActionListener(new ActionListener() {
				public void actionPerformed(ActionEvent arg0) {

					min = ran2+1;// Up버튼을 눌렀을시 컴퓨터가 물어본 ran2값을 min에 저장

					ran2 = (ran2 + max) / 2 + 1; // 중간 값을 구함

					count2++;

					comoutput.append(ran2 + "이(가) 정답입니까??\n");
					comoutput.setCaretPosition(comoutput.getText().length());// 스크롤 맨아래로
																				 
					btnUp.setEnabled(false); // 버튼 비활성화
					btnDown.setEnabled(false);
				}
			});
			btnUp.setBounds(161, 177, 97, 23);
		}
		return btnUp;
	}

	private JButton getBtnDown() {
		if (btnDown == null) {
			btnDown = new JButton("Down");
			btnDown.addActionListener(new ActionListener() {
				public void actionPerformed(ActionEvent arg0) {

					max = ran2-1; // down버튼 클릭시 ran2를 max에 저장

					if (min == 1) {// 처음 min이 1일때 실행
						ran2 = ran2 / 2;
					} else {// min값이 바뀌면 실행
						ran2 = (ran2 + min) / 2;
					}

					count2++;

					comoutput.append(ran2 + "이(가) 정답입니까??\n");
					comoutput.setCaretPosition(comoutput.getText().length());// 스크롤
																				// 맨아래로
					btnUp.setEnabled(false); // 버튼 비활성화
					btnDown.setEnabled(false);

				}

			});
			btnDown.setBounds(161, 210, 97, 23);
		}
		return btnDown;
	}

	private JButton getBtnYes() {
		if (btnYes == null) {
			btnYes = new JButton("Yes");
			btnYes.addActionListener(new ActionListener() {
				public void actionPerformed(ActionEvent arg0) {
					comoutput.append(count2 + "번 째에 정답");
					JOptionPane.showMessageDialog(null, count2+ "번 째에 정답입니다.!!");
					textField_1.setText("");
				}
			});
			btnYes.setBounds(33, 177, 97, 23);
		}
		return btnYes;
	}

	private JButton getBtnNo() {
		if (btnNo == null) {
			btnNo = new JButton("No");
			btnNo.addActionListener(new ActionListener() {
				public void actionPerformed(ActionEvent arg0) {
					comoutput.append(ran2 + "보다 up?down?\n");
					btnUp.setEnabled(true);// 버튼 활성화
					btnDown.setEnabled(true);
				}
			});
			btnNo.setBounds(33, 210, 97, 23);
		}
		return btnNo;
	}

	private JTextArea getComoutput() {
		if (comoutput == null) {
			comoutput = new JTextArea();
		}
		return comoutput;
	}

	private JScrollPane getScrollPane_3() {
		if (scrollPane_3 == null) {
			scrollPane_3 = new JScrollPane();
			scrollPane_3.setBounds(10, 30, 280, 112);
			scrollPane_3.setViewportView(getRes());
		}
		return scrollPane_3;
	}

	private JTextArea getRes() {
		if (res == null) {
			res = new JTextArea();
		}
		return res;
	}

	private JButton getButton_3() {
		if (button_3 == null) {
			button_3 = new JButton("가위");
			button_3.addActionListener(new ActionListener() {
				public void actionPerformed(ActionEvent arg0) {
					int m=1; // 묵
					int j=2; // 찌
					int b=3; // 빠
					int k=(int)(Math.random()*3)+1; // 난수 발생
					if(b==k){
						res.append("사용자 : 가위");  // res 출력창
						res.append("\n");
						res.append("컴퓨터 : 보자기");
						res.append("\n");
						turn.setText("사용자"); // 선공 후공에서 선공창을 나타냄
						res.append("\n");
					}
					else if(m==k){
							res.append("사용자 : 가위");
							res.append("\n");
							res.append("컴퓨터 : 바위");
							turn.setText("컴퓨터");
							res.append("\n");
							res.append("\n");
					}else if(j==k&&turn.getText().equals("")){ // 컴퓨터(k)와 사용자가 비긴경우
						res.append("사용자 : 가위");
						res.append("\n");
						res.append("컴퓨터 : 가위");
						res.append("\n");
						turn.setText("");
						JOptionPane.showMessageDialog(null,"DRAW!!.RE Strart!!.");
					}
					else if(j==k&&turn.getText().equals("사용자")){//사용자 선공일때
						res.append("사용자 : 가위");
						res.append("\n");
						res.append("컴퓨터 : 가위");
						turn.setText("");
						res.append("\n");
						JOptionPane.showMessageDialog(null,"YOU WIN.!!");
					}
					else if(j==k&&turn.getText().equals("컴퓨터")){//컴퓨터가 선공일때
						res.append("사용자 : 가위");
						res.append("\n");
						res.append("컴퓨터 : 가위");
						res.append("\n");
						turn.setText("");
						JOptionPane.showMessageDialog(null,"YOU LOSE.!!");
						
					}
				}
			});
			button_3.setBounds(12, 170, 97, 23);
		}
		return button_3;
	}

	private JButton getButton_4() {
		if (button_4 == null) {
			button_4 = new JButton("바위");
			button_4.addActionListener(new ActionListener() {
				public void actionPerformed(ActionEvent arg0) {
					int m = 1;
					int j = 2;
					int b = 3;
					int k = (int) (Math.random() * 3) + 1;
					if (j == k) {
						res.append("사용자 : 바위");
						res.append("\n");
						res.append("컴퓨터 : 가위");
						res.append("\n");
						turn.setText("사용자");
						res.append("\n");
					} else if (b == k) {
						res.append("사용자 : 바위");
						res.append("\n");
						res.append("컴퓨터 : 보자기");
						res.append("\n");
						turn.setText("컴퓨터");
						res.append("\n");
					} else if (m == k && turn.getText().equals("사용자")) {
						res.append("사용자 : 바위");
						res.append("\n");
						res.append("컴퓨터 : 바위");
						res.append("\n");
						turn.setText("");
						JOptionPane.showMessageDialog(null, "YOU WIN.!!");
						res.append("\n");
					} else if (m == k && turn.getText().equals("컴퓨터")) {
						res.append("사용자 : 바위");
						res.append("\n");
						res.append("컴퓨터 : 바위");
						res.append("\n");
						turn.setText("");
						JOptionPane.showMessageDialog(null, "YOU LOSE!!.");
						res.append("\n");
					} else if (m == k && turn.getText().equals("")) {
						res.append("사용자 : 바위");
						res.append("\n");
						res.append("컴퓨터 : 바위");
						res.append("\n");
						turn.setText("");
						JOptionPane
								.showMessageDialog(null, "DRAW!!.RE Start!!");
						res.append("\n");
					}
				}
			});
			button_4.setBounds(12, 203, 97, 23);
		}
		return button_4;
	}

	private JButton getButton_5() {
		if (button_5 == null) {
			button_5 = new JButton("보");
			button_5.addActionListener(new ActionListener() {
				public void actionPerformed(ActionEvent arg0) {
					int m = 1;
					int j = 2;
					int b = 3;
					int k = (int) (Math.random() * 3) + 1;
					if (m == k) {
						res.append("사용자 : 보자기");
						res.append("\n");
						res.append("컴퓨터 : 바위");
						res.append("\n");
						turn.setText("사용자");
						res.append("사용자가 이겼습니다.");
						res.append("\n");
					} else if (j == k) {
						res.append("사용자 : 보");
						res.append("\n");
						res.append("컴퓨터 : 가위");
						res.append("\n");
						turn.setText("컴퓨터");
						res.append("컴퓨터가 이겼습니다.");
						res.append("\n");
					} else if (b == k && turn.getText().equals("사용자")) {
						res.append("사용자 : 보");
						res.append("\n");
						turn.setText("");
						res.append("컴퓨터 : 보");
						res.append("\n");
						JOptionPane.showMessageDialog(null, "YOU WIN!!!.");
					} else if (b == k && turn.getText().equals("컴퓨터")) {
						res.append("사용자 : 보");
						res.append("\n");
						res.append("컴퓨터 : 보");
						res.append("\n");
						turn.setText("");
						JOptionPane.showMessageDialog(null, "컴퓨터가 이겼습니다.");
						res.append("\n");
					} else if (b == k && turn.getText().equals("")) {
						res.append("사용자 : 보자기");
						res.append("\n");
						res.append("컴퓨터 : 보자기");
						res.append("\n");
						turn.setText("");
						JOptionPane.showMessageDialog(null, "비겼습니다. 다시 눌러주세요");
						res.append("\n");
					}
				}
			});
			button_5.setBounds(12, 236, 97, 23);
		}
		return button_5;
	}

	private JLabel getLblNewLabel_2() {
		if (lblNewLabel_2 == null) {
			lblNewLabel_2 = new JLabel("TURN");
			lblNewLabel_2.setBounds(165, 178, 57, 15);
		}
		return lblNewLabel_2;
	}

	private JTextArea getTurn() {
		if (turn == null) {
			turn = new JTextArea();
			turn.setBounds(163, 203, 112, 24);
		}
		return turn;
	}

	private JButton getBtnNewButton_3() {
		if (btnNewButton_3 == null) {
			btnNewButton_3 = new JButton("초기화");
			btnNewButton_3.addActionListener(new ActionListener() {
				public void actionPerformed(ActionEvent arg0) {

					textField_1.setText("");
					comoutput.setText("");

					max = 1000;
					min = 1;
					ran2 = max / 2;
					count2 = 0;
					
					textField_1.requestFocus(); // 커서이동

				}
			});
			btnNewButton_3.setBounds(161, 26, 97, 23);
		}
		return btnNewButton_3;
	}
}
